package com.travel.travelServer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.travel.travelServer.entity.Collect;
import com.travel.travelServer.mapper.CollectMapper;
import com.travel.travelServer.mapper.GoodsMapper;
import com.travel.travelServer.mapper.UserMapper;
import com.travel.travelServer.service.CollectService;
import com.travel.travelServer.utils.BaseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * (Collect)表服务实现类
 *
 * @author makejava
 * @since 2024-03-01 16:08:00
 */
@Service("collectService")
public class CollectServiceImpl extends BaseService<CollectMapper, Collect> implements CollectService {

    @Resource
    private GoodsMapper fruitDao;

    @Resource
    private UserMapper userDao;

    @Override
    public Page<Collect> page(Page<Collect> page, Map<String, Object> conditions) {
        Long userId = getCurrentUser().getId();
        LambdaQueryWrapper<Collect> collectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        collectLambdaQueryWrapper.eq(Collect::getUserId, userId);
        Page<Collect> collectPage = super.page(page, collectLambdaQueryWrapper);
        collectPage.getRecords().stream().forEach(collect -> {
            if (Objects.nonNull(collect.getCollectId())){
                collect.setGoods(fruitDao.selectById(collect.getCollectId()));
            }
            if (Objects.nonNull(collect.getUserId())){
                collect.setUser(userDao.selectById(collect.getUserId()));
            }
        });
        return collectPage;
    }

    @Override
    public Collect getById(Long id) {
        Collect collect = super.getById(id);
        if (Objects.nonNull(collect.getCollectId())){
            collect.setGoods(fruitDao.selectById(collect.getCollectId()));
        }
        if (Objects.nonNull(collect.getUserId())){
            collect.setUser(userDao.selectById(collect.getUserId()));
        }
        return collect;
    }

    @Override
    public Boolean add(Collect pojo) {
        Long userId = getCurrentUser().getId();
        pojo.setUserId(userId);
        return super.save(pojo);
    }

    @Override
    public Boolean update(Collect pojo) {
        return super.updateById(pojo);
    }

    @Override
    public Boolean removeOne(Long id) {
        return super.removeById(id);
    }

    @Override
    public Boolean batchRemove(List<Long> asList) {
        return super.removeByIds(asList);
    }

    @Override
    public Boolean quCollect(Collect collect) {
        Long userId = getCurrentUser().getId();
        LambdaQueryWrapper<Collect> collectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        collectLambdaQueryWrapper.eq(Collect::getUserId, userId);
        collectLambdaQueryWrapper.eq(Collect::getCollectId, collect.getCollectId());
        Collect dbCollect = super.getOne(collectLambdaQueryWrapper);
        return super.removeById(dbCollect.getId());
    }
}

